<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=array('0'=>'未显示','1'=>'已显示','2'=>'已驳回');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_chatgpt_wenda',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['msg']=cfc('tools')->substr($v['msg'],0,38);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'问答库'),
			array('op'=>'edit','title'=>'添加问答'),
			array('op'=>'listadd','title'=>'导入问答'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：问答优先匹配！','list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'问题','type'=>'string'),
			array('name'=>'msg','title'=>'答案','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'问题','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_chatgpt_wenda',array('id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'问答库',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回问答库'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'问题','type'=>'string'),
			array('name'=>'msg','title'=>'答案','type'=>'text'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$postdata['latlng']=ijson_encode($postdata['latlng']);
	$data=array_fields(array('msg','status','title','paixu'),$postdata);	
	if(!$data['title']){
		exi('请填写问题','error');
	}
	if(!$data['msg']){
		exi('请填写答案','error');
	}
	if($id){
		pdo_update('jy_chatgpt_wenda',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_chatgpt_wenda',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_chatgpt_wenda',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='listadd'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('wenda')->daoru($postdata);
		exi('导入成功','','/wenda/');
	}
	$return['item']=array();
	$htmls=array (
		  'type' => 'edit',
			 'tabs'=>array(
				array('op'=>'list','title'=>'返回问答库'),
			),
		  'edit' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：在导入过程中，请不要进行任何操作!请选择本插件导出的问答库表，其他表不支持</red>'),
				array ('name' => 'excel','type' => 'upfile','title' => '上传CSV','help'=>''),
		  ),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
    if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$list=pdo_getall('jy_chatgpt_wenda',$params);
	foreach($list as &$v){
		$v['title']=base64_encode($v['title']);
		$v['msg']=base64_encode($v['msg']);
	}
	$header=array(
		'paixu'=>'排序',
		'title'=>'问题(已加密)',
		'msg'=>'答案(已加密)',
	);
	cfc('tools')->export2excel($header,$list,'答案库');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_chatgpt_wenda',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_chatgpt_wenda',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
?>